package com.personal.portal.admin.controller;

import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Project;
import com.personal.portal.admin.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目控制器
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Tag(name = "项目", description = "项目信息管理接口")
@RestController
@RequestMapping("/admin/project")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ProjectController {

    @Resource
    private ProjectService projectService;

    /**
     * 根据ID查询项目
     */
    @Operation(summary = "根据ID查询项目", description = "根据项目ID查询详细信息")
    @GetMapping("/{id}")
    public Result<Project> queryById(
            @Parameter(description = "项目ID", required = true) 
            @PathVariable Long id) {
        try {
            Project project = projectService.queryById(id);
            if (project != null) {
                return Result.success(project);
            } else {
                return Result.error("项目信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询项目信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据条件查询项目列表
     */
    @Operation(summary = "根据条件查询项目列表", description = "根据项目名称、描述等条件查询项目信息")
    @PostMapping("/list")
    public Result<List<Project>> queryList(
            @Parameter(description = "项目查询条件") 
            @RequestBody Project project) {
        try {
            List<Project> projectList = projectService.queryList(project);
            return Result.success(projectList);
        } catch (Exception e) {
            return Result.error("查询项目列表失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询项目
     */
    @Operation(summary = "分页查询项目", description = "支持分页和条件查询的项目信息")
    @PostMapping("/page")
    public Result<Result.PageResult<Project>> queryPage(
            @Parameter(description = "页码，从1开始", example = "1") 
            @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "10") 
            @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "项目查询条件") 
            @RequestBody Project project) {
        try {
            Result.PageResult<Project> pageResult = projectService.queryPage(pageNum, pageSize, project);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("分页查询项目失败: " + e.getMessage());
        }
    }

    /**
     * 创建项目
     */
    @Operation(summary = "创建项目", description = "新增一条项目信息")
    @PostMapping
    public Result<Long> create(
            @Parameter(description = "项目信息", required = true) 
            @RequestBody Project project) {
        try {
            int result = projectService.insert(project);
            if (result > 0) {
                return Result.success("创建项目成功", project.getId());
            } else {
                return Result.error("创建项目失败");
            }
        } catch (Exception e) {
            return Result.error("创建项目失败: " + e.getMessage());
        }
    }

    /**
     * 更新项目
     */
    @Operation(summary = "更新项目", description = "更新指定ID的项目信息")
    @PutMapping("/{id}")
    public Result<Long> update(
            @Parameter(description = "项目ID", required = true) 
            @PathVariable Long id,
            @Parameter(description = "项目信息", required = true) 
            @RequestBody Project project) {
        try {
            project.setId(id);
            int result = projectService.update(project);
            if (result > 0) {
                return Result.success("更新项目成功", id);
            } else {
                return Result.error("更新项目失败");
            }
        } catch (Exception e) {
            return Result.error("更新项目失败: " + e.getMessage());
        }
    }

    /**
     * 删除项目
     */
    @Operation(summary = "删除项目", description = "根据ID删除指定的项目信息")
    @DeleteMapping("/{id}")
    public Result<Long> delete(
            @Parameter(description = "项目ID", required = true) 
            @PathVariable Long id) {
        try {
            int result = projectService.deleteById(id);
            if (result > 0) {
                return Result.success("删除项目成功", id);
            } else {
                return Result.error("删除项目失败");
            }
        } catch (Exception e) {
            return Result.error("删除项目失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除项目
     */
    @Operation(summary = "批量删除项目", description = "根据ID列表批量删除项目信息")
    @DeleteMapping("/batch")
    public Result<List<Long>> deleteBatch(
            @Parameter(description = "项目ID列表", required = true) 
            @RequestBody List<Long> ids) {
        try {
            List<Long> successIds = projectService.deleteBatch(ids);
            return Result.success(
                String.format("成功删除 %d 条记录", successIds.size()), successIds);
        } catch (Exception e) {
            return Result.error("批量删除项目失败: " + e.getMessage());
        }
    }
}